package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Override
    /**
     * 分页查询护理等级
     */
    public PageResponse<NursingLevelVo> selectPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //1.设置分页查询页码
        PageHelper.startPage(pageNum, pageSize);
        //2.Mapper层完成分页查询
        Page<NursingLevelVo> page = nursingLevelMapper.selectPage(name,status);
        //pageResponse的封装 以前的写法是 return PageResult(page.getTotal,page.getResult)
        return PageResponse.of(page,NursingLevelVo.class);
    }

    /**
     * 新增护理等级
     * @param nursingLevelDto
     */
    @Override
    public void add(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = new NursingLevel();

        BeanUtils.copyProperties(nursingLevelDto, nursingLevel);

        nursingLevel.setLplanId(nursingLevelDto.getPlanId());

        nursingLevel.setCreateTime(LocalDateTime.now());
        nursingLevel.setUpdateTime(LocalDateTime.now());

        nursingLevelMapper.insert(nursingLevel);
    }

    /**
     * 根据Id查询等级
     * @param id
     * @return
     */
    @Override
    public NursingLevelVo getById(Integer id) {
        NursingLevelVo nursingLevelVo = nursingLevelMapper.getById(id);
        return nursingLevelVo;
    }

    /**
     * 新增护理等级
     * @param nursingLevelDto
     */
    @Override
    public void update(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtils.copyProperties(nursingLevelDto, nursingLevel);
        nursingLevel.setLplanId(nursingLevelDto.getPlanId());

        nursingLevelMapper.update(nursingLevel);
    }

    /**
     * 根据Id删除
     * @param id
     */
    @Override
    public void delById(Integer id) {
        nursingLevelMapper.delById(id);
    }

    /**
     * 启用或者禁用
     * @param status
     * @param id
     */
    @Override
    public void startOrstop(Integer status, Integer id) {
        NursingLevel nursingLevel = new NursingLevel();
        nursingLevel.setId(id);
        nursingLevel.setStatus(status);
        nursingLevelMapper.update(nursingLevel);
    }
}
